#ifndef GX_FR_EDITOR_H
#define GX_FR_EDITOR_H

#include "Command.h"
#include "State.h"
#include "Basic/GxLibBasic.h"
#include "Scene.h"
#include "LibGL/GxUI.h"
#include "ViewGrid.h"
#include "MathExtend.h"
#include "FileControl.h"
#include "Windows/GxWinForm.h"

namespace FinalRendering
{
	namespace Editor
	{
		class State;
		class Command;

		using namespace GxRendering::UI;
		using namespace GxLibBasic;

		class ViewGrid;

		class EditorPreference : public Object
		{
		public:
			int MinScrGridSize;
			float PerspectiveViewMousePrecision;
			float CameraMinSpeed, CameraMaxSpeed, CameraAcceleration, CameraTurnAngle;
			GxRendering::UI::Color GridColor[MaxGridColorLevels];

			String WorkPath;

			String GetPreferenceFileName();

			EditorPreference();
			~EditorPreference();
			void LoadPreference();
			void SavePreference();
		};

		class WorldEditorSingleton : public Object
		{
		private:
			int cmdTail;
			RefPtr<FileControl> file; 
			Form * mainForm;
			void SceneGetMaterialPath(String & path);
		public:
			RefPtr<GxWinGUI::BaseForm> dlgCompile;
			enum _SelectMode
			{
				smObject, smGroup
			};
			_SelectMode SelectMode;
			bool EditTexcoord;
			bool SnapToGrid;
			String EntityClassName;
			EditorPreference Preference;
			Array<Selection> Selection;
			Array<RefPtr<State>> StateStack;
			RefPtr<Scene> Scene;
			ViewGrid * Views;
			Array<RefPtr<Command>> Commands;
			int BrushEdges;
			enum _BrushType
			{
				btBox, btCylinder, btCone, btArc
			};
			_BrushType BrushType;
			Event<> OnSelectionChanged;
			void PushState(State * state);
			void PopState();
			State * GetCurrentState();
			void Init(Form * form, UIEntry * ui);
			void Finalize();
			void NewDoc();
			void Execute(Command * cmd);
			void Undo();
			void Redo();
			void Delete();
			void Cut();
			void Copy();
			void Paste();
			void SelectAll();
			void SetBrushFlag(bool ignore);
			bool CanUndo();
			bool CanRedo();
			void Group();
			void Ungroup();
			void ClearUnusedGroups();
			void CancelSelection();
			void SelectionChanged();
			void SetState(State * state);
			void UpdateViews();
			void Carve();
			void MakeHollow();
			bool Save();
			bool SaveAs();
			bool Open();
			bool New();
			bool Close();
			Form * GetMainForm();
			void TextureAdjust_TreatAsOne();
			String GetMaterialPath();
			void ProcessLocalAccelerator(UIKeyEventArgs & e);
		};

		extern WorldEditorSingleton * WorldEditor;

		void InitEditor(Form * form, UIEntry * ui);
		void FinalizeEditor();
	}
}

#endif